<!--
 * @description  : 异常处理文书审批
 * @descriptionDetail: 异常处理文书审批
 * @copyright    : 浙江烟草
 * @author       : yzp
 * @create       : 2022-09-27 09:58:58
-->
<template>
  <view>
    <u-form
      :model="formData"
      :error-type="errorType"
      label-position="top"
      ref="uForm"
    >
      <u-card
        padding="0"
        margin="0"
        :border="false"
        :show-foot="false"
        v-if="step=='02'"
        :head-style="{ padding: '2px' }"
      >
        <template #head>
          <u-row style="padding-top: 8px; padding-bottom: 10px">
            <u-col span="12">
              <view class="card-head">
                结论定性
              </view>
            </u-col>
          </u-row>
        </template>
        <template #body>
          <u-form-item
            :label-width="labelWidth"
            label="调查结论与结果定性"
            :required="step=='02'"
            prop="supvResultName"
          >
            <z-dict-select
              :readOnly="step!='02'"
              :dictType="$u.dic.DIC_KEYS.I_WARN_CONC_STATUS"
              v-model="formData.supvResultName"
              @confirm="handlequalResult"
            ></z-dict-select>
          </u-form-item>
          <u-form-item
            :label-width="labelWidth"
            label="问题类型"
            prop="qualResultName"
            v-if="formData.supvResultName!=='正常'"
          >
            <z-dict-select
              v-if="formData.supvResultName=='隐患'"
              :readOnly="step!='02'"
              :dictType="$u.dic.DIC_KEYS.I_WR_BUS_HIDDEN_TYPE"   
              v-model="formData.qualResultName"
              @confirm="handlequalResult"
            ></z-dict-select>
            <z-dict-select
              v-else-if="formData.supvResultName=='问题'"
              :readOnly="step!='02'"
              :dictType="$u.dic.DIC_KEYS.I_WR_BUS_PROBLEM_TYPE"
              :questionTab="questionTab"
              v-model="formData.qualResultName"
              @confirm="handlequalResult"
            ></z-dict-select>
          </u-form-item>
          <u-form-item
            :label-width="labelWidth"
            label="问题隐患处理"
            v-show="formData.supvResultName!=='正常'"
            prop="dealTypeName"
            :required="step=='02'&&formData.supvResultName!=='正常'"
          >
            <u-radio-group wrap v-model="formData.dealType" @change="handleDealTypeName">
              <u-radio 
                v-for="(item, index) in dealTypeList" 
                :key="index"
                shape="square"
                :name="item.bcbCode"
                :disabled="item.disabled ||step!='02'"
              >
                {{ item.bcbName }}
              </u-radio>
            </u-radio-group>
          </u-form-item>
        </template>
      </u-card>
      <u-card
        padding="0"
        margin="0"
        :border="false"
        :show-foot="false"
        v-if="step=='03'"
        :head-style="{ padding: '2px' }"
      >
        <template #head>
          <u-row style="padding-top: 8px; padding-bottom: 10px">
            <u-col span="12">
              <view class="card-head">
                审核
              </view>
            </u-col>
          </u-row>
        </template>
        <template #body>
          <!-- 审核 -->
          <u-form-item
            label="审核意见"
            prop="condition03"
            :required="step=='03'"
          >
            <u-input
              :disabled="step!=='03'"
              type="textarea"
              placeholder="请输入"
              v-model="formData.condition03"
            />
          </u-form-item>
          <u-row>
            <u-col span="6">
              <u-icon name="account" :label="formData.userName03"></u-icon>
            </u-col>
            <u-col span="6">
              <u-row>
                <u-col span="1">
                  <u-icon name="calendar" class="calendar"></u-icon>
                </u-col>
                <u-col span="11">
                  <z-date
                    placeholder="请输入"
                    format="yyyy-MM-dd"
                    v-model="formData.feedbackDate03"
                    readOnly
                  />
                </u-col>
              </u-row>
            </u-col>
          </u-row>
        </template>
      </u-card>
    </u-form>
    <!-- 流程操作 -->
    <view class="footer" v-if="step!=='view' && step!=='edit'">
      <u-row gutter="16">
        <u-col span="6">
          <u-button type="error" @tap="handleReject">
            退回
          </u-button>
        </u-col>
        <u-col span="6">
          <u-button type="primary" @tap="handleAgree">
            同意
          </u-button>
        </u-col>
      </u-row>
    </view>
    <!-- 流程人员选择 --->
    <u-popup v-model="showPerson" mode="bottom" :mask-close-able="true">
      <view class="popup-header-item">
        人员选择
        <span class="header-btn" @tap="handlePersonSelected">确定</span>
      </view>
      <view style="height:50vh;padding-left:5%;">
        <u-checkbox-group :wrap="true" @change="checkboxGroupChange">
          <u-checkbox 
            v-for="(item, index) in persons" 
            :key="index" 
            v-model="item.checked"
            :name="item.personId"
          >
            {{ item.name }}
          </u-checkbox>
        </u-checkbox-group>
      </view>
    </u-popup>
  </view>
</template>

<script>
import store from '@/store';
import approval from "@/service/internal/approval.service.js";
import util from "@/common/util.js";
import commonService from "@/service/internal/common.service";
import { ZDictSelect, ZDate,  } from "@/components";
import {DICT_ENUM,} from "@/service/internal/mapping/enum.js";
import { object, } from '@dcloudio/vue-cli-plugin-uni/packages/postcss/tags';
export default {
  components: {  ZDictSelect,ZDate, },
  props:{
    docIns:{
      type:Object,
      default:() => {},
    },
    step:{
      type:String,
      default:"",
    },
    stepCode:{
      type:String,
      default:"",
    },
    flowFormCode:{
      type:String,
      default:"",
    },
    approve: {
      type: Array,
      default: () => [],
    },
    documentDoc:{
      type:Object,
      default:() => ({}),
    },
    bizUuid: {
      type:String,
      default:"",
    },
  },
  data() {
    return {
      approveList:[],
      showPerson:false,
      clueShow: false,
      isdisable: true, // 全部禁用
      isresults:true, // 结果定性阶段
      labelWidth: 180,
      detailData:{},
      test1: "",
      formData: {
        excName: "",
        excRegDate: "", // 登记日期
        supvPerson: "", // 监管人员
        excTypeName: "", // 异常分类
        excLinkName: "", // 经营环节
        excDate: "", // 问题日期
        excSourceName: "", // 异常来源
        clueType: "", // 线索类型
        excDesc:"", // 异常情况描述
        excHistDesc:"",// 涉及调查对象基本情况
        surveyAdvise:"",// 调查意见
        surveyTypeName: "",// 调查方式
        fbLimitDate:"",// 要求反馈时间
        surveyPersonName:"",// 调查人员
        isAudit:false,// 生成实地核查任务
        surveyTime:"",// 内管调查时间
        surveyPerson:"",// 内管调查人
        surveyInfo:"",// 内管调查情况
        supvResult:"",// 调查结论与结果定性
        qualResult:"",// 问题类型
        qualTime:"",// 定性时间
        userName02:"",// 定性人
        feedbackDate02:"",
        dealType:"",// 建议处理方式
        condition02:"同意", // 意见
        condition03:"同意",// 审核意见
        userName03:"",// 审核人
        feedbackDate03:"",// 审核时间
        supvResultName:"",
        qualResultName:"",
        dealTypeName:"",
        checkboxs: true,
        questionTab:'',
      },
      radiolist1: [
        {
          name: true,
          disabled: false,
        },
      ],
      rules: {
        supvResultName:[
          {
            required: false,
            message: '请填写内容',
            trigger:'change',
          },
        ],
        dealTypeName:[
          {
            required: false,
            message: '请填写内容',
            trigger:'change',
          },
        ],
        qualResultName:[
          {
            required: false,
            message: '请填写内容',
            trigger:'change',
          },
        ],
        condition02:[
          {
            required: false,
            message: '请填写内容',
            trigger:'change',
          },
        ],
        condition03:[
          {
            required: false,
            message: '请填写内容',
            trigger:'change',
          },
        ],
      },
      uploadFile: commonService.uploadFile,
      delUrl: commonService.deleteFile,
      errorType: ["message",],
      checkedPerson:[],// 复选框选中后的数据
      persons:[],
      dealTypeList:[],// 问题隐患数据
    };
  },
  watch:{
    docIns: {
      deep:true,
      immediate: true,
      handler(v) {
        console.log("审批文书信息", v, this.step);
        // this.formData = {...v,}
        if (Object.keys(v).length > 0) {
        
          Object.keys(v).forEach(k => this.formData[k] = v[k])
          this.questionTab = v.excType
          console.log('限制',this.questionTab)
          this.formData.supvResultName = this.getDictName(this.$u.dic.DIC_KEYS.I_WARN_CONC_STATUS,this.formData.supvResult)
          if( this.formData.supvResultName == '隐患'){
            this.formData.qualResultName = this.getDictName(this.$u.dic.DIC_KEYS.I_WR_BUS_HIDDEN_TYPE,this.formData.qualResult)
          }else{
            this.formData.qualResultName = this.getDictName(this.$u.dic.DIC_KEYS.I_WR_BUS_PROBLEM_TYPE,this.formData.qualResult)
          }
          this.formData.dealTypeName = this.getDictName(this.$u.dic.DIC_KEYS.I_EXC_DEAL_TYPE,this.formData.dealType)
        }
      },
    },
    formData: {
      immediate: true,
      handler(v) {
        console.log("formData值变了", v);
      },
    },
    documentDoc:{
      deep:true,
      immediate: true,
      handler(v) {
        console.log("byID", v);
      },
    },
    'formData.supvResultName':{
      handler: function (value) {
        if(this.step == '02'){
          if (value !== '正常') {
            this.rules.dealTypeName[0].required = true
          } else {
            this.rules.dealTypeName[0].required = false
          }
        }
      },
    },
    approve:{
      deep:true,
      handler(v) {
        this.approveList = [...v,]
        console.log("流程信息", v,this.approveList);
        let userInfo = this.$storage.get(this.$rp.common.USER).user.personName
        let toDay = util.dateUtils.dateFormat('yyyy-MM-dd',new Date())
        if(this.step == '02'){
          this.rules.supvResultName[0].required = true
         
          // this.rules.condition02[0].required = true
          this.formData.userName03 = ''
          this.formData.feedbackDate03 = ''
        }else{
          this.rules.condition03[0].required = true
          this.formData.userName03 = this.filterApproveList("03",'approvePersonName',userInfo) 
          this.formData.feedbackDate03 = this.filterApproveList("03",'approveDate',toDay)
        }
        this.formData.userName02 = this.filterApproveList("02",'approvePersonName',userInfo)
        this.formData.feedbackDate02 = this.filterApproveList("02",'approveDate',toDay)
        this.formData.excRegDate = util.dateUtils.dateFormat('yyyy-MM-dd',new Date(this.formData.excRegDate))
        this.formData.excDate = util.dateUtils.dateFormat('yyyy-MM-dd',new Date(this.formData.excDate))
        this.formData.fbLimitDate = util.dateUtils.dateFormat('yyyy-MM-dd',new Date(this.formData.fbLimitDate))
        this.formData.surveyTime = util.dateUtils.dateFormat('yyyy-MM-dd',new Date(this.formData.surveyTime))
      },
    },
  },
  created(){
    this.dealTypeList = this.$u.dic.getByDictCode(this.$u.dic.DIC_KEYS.I_EXC_DEAL_TYPE);
    console.log(this.dealTypeList)
  },
  mounted(){
    console.log("状态",this.step)

    console.log(this.detailData, this.formData);
   
    this.$refs.uForm.setRules(this.rules);
  },
  changeHeight(){ 
    this.$refs.consitem.init();
  },
  onLoad(){
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {
    filterApproveList(step,stepValue,userInfo){
      return  this.approveList.filter(item => item.gfaCode == step)[0][stepValue] ? this.approveList.filter(item => item.gfaCode == step)[0][stepValue] : userInfo ? userInfo : ""
    },
    getDictName(value,dicName){
      if (!value) return ''
      if (!dicName) return ''
      return store.state.dicts[dicName].filter(d => d.bcbCode === value)[0].bcbName;
    },
    radioChange(item) {},
    handleSwitch(sw) {
      this.radioCheckWrap = sw;
    },
    handleDictConfirm(item){
      console.log("调用选中", item);

    },
    handleDealTypeName(item){
      console.log("调用选中", item);
      this.formData.dealType = item.value;
    },
    // 字典选中确认
    handlequalResult(item,arr) {
      console.log("调用方法",item)
      if(item.bctPhName === this.$u.dic.DIC_KEYS.I_WARN_CONC_STATUS){
        // this.questionTab = item.value
        console.log("调用选中123", item,arr);
        this.formData.supvResult = item.value;
        this.formData.supvResultName = item.label;
        this.$set(this.formData, 'supvResultName', item.label)
        if(item.value == DICT_ENUM.I_WARN_CONC_STATUS.I_WARN_CONC_STATUS2){ // 设置问题隐患处理默认值
          this.formData.dealType = DICT_ENUM.I_EXC_DEAL_TYPE.I_EXC_DEAL_TYPE01;
          this.formData.dealTypeName = DICT_ENUM.I_EXC_DEAL_TYPE.I_EXC_DEAL_TYPE01_CN;
          this.dealTypeList = this.$u.dic.getByDictCode(this.$u.dic.DIC_KEYS.I_EXC_DEAL_TYPE).map(d => {
            if(d.bcbCode == DICT_ENUM.I_EXC_DEAL_TYPE.I_EXC_DEAL_TYPE01){
              d.disabled = false;
            }else{
              d.disabled = true;
            }
            return d;
          });
        }else{
          this.formData.dealType = DICT_ENUM.I_EXC_DEAL_TYPE.I_EXC_DEAL_TYPE02;
          this.formData.dealTypeName = DICT_ENUM.I_EXC_DEAL_TYPE.I_EXC_DEAL_TYPE02_CN;
          this.dealTypeList = this.$u.dic.getByDictCode(this.$u.dic.DIC_KEYS.I_EXC_DEAL_TYPE).map(d => {
            if(d.bcbCode != DICT_ENUM.I_EXC_DEAL_TYPE.I_EXC_DEAL_TYPE01){
              d.disabled = false;
            }else{
              d.disabled = true;
            }
            return d;

          });
        }
        console.log("调用选中", this.dealTypeList);
        // this.$set(this.formData, {supvResult: item.value, supvResultName: item.label,});
        // this.test1 = item.label;
      }else if(item.bctPhName === this.$u.dic.DIC_KEYS.I_WR_BUS_PROBLEM_TYPE || item.bctPhName === this.$u.dic.DIC_KEYS.I_WR_BUS_HIDDEN_TYPE){      
        this.formData.qualResult = item.value
        this.formData.qualResultName = item.label;
        this.$set(this.formData, 'qualResultName', item.label)
        console.log("调用选中222", this.formData.qualResultName,arr);
      }else if(item.bctPhName === this.$u.dic.DIC_KEYS.I_EXC_DEAL_TYPE){
        console.log("调用选中", item);
        this.formData.dealType = item.value
        this.formData.dealTypeName = item.label;
        this.$set(this.formData, 'dealTypeName', item.label)
        console.log("调用选中333", this.formData.dealTypeName);
      }
      // this.$forceUpdate()
      console.log("888",this.formData);
    },
    // 点击同意按钮
    handleAgree(){ 
      if(this.step == '02'){
        approval.selectprocessParson(1,100,this.flowFormCode,this.stepCode,this.documentDoc.docInsUuid,this.documentDoc.isReport ? "1" : "0").then(res => {
          this.showPerson = true
          if(res.data && res.data.length > 0){
            this.persons = res.data.map(item => {
              return {
                ...item,
                name:"姓名:" + item.personName + " " + "电话:" + (item.phone ? item.phone : '无') ,
                checked: false,
                disabled: false,
              }
            })
          }
        })    
      }else{
        this.handlePersonSelected()
      }
    },
    // 点击选中
    checkboxGroupChange(e){
      this.checkedPerson = e
    },
    handlePersonSelected(){
      this.$modal.showLoading("加载中...");
      if(this.checkedPerson.length <= 0 && this.step !== '03'){
        this.$u.toast('请选择人员')
        return false
      }
      let toDay = util.dateUtils.dateFormat('yyyy-MM-dd',new Date())
      let params = {
        gfaCode:this.step,
        nextPersons:this.checkedPerson,
        approvePersonUuid:this.$storage.get(this.$rp.common.USER).user.personUuid,
      }
      this.$refs.uForm.validate(valid => {
        if (valid) {
          console.log('验证通过');
          switch(this.step){
          case "02":
            // params.approveInfo = this.formData.condition02
            params.approveInfo = '同意'
            params.approveDate = this.formData.feedbackDate02
            break;
          case "03":
            params.approveInfo = this.formData.condition03
            params.approveDate = this.formData.feedbackDate03
            break;
          default:
            params.approveInfo = "同意"
            params.approveDate = toDay
          }
          console.log(params);
          console.log(this.formData);
          approval.abnormalAgree(this.formData,params).then(res => {
            if(res.success){
              this.showPerson = false
              this.$u.toast(res.msg)
              this.backPrev();
              
            }else{
              this.$u.toast('审核失败')
            }
            this.$modal.hideLoading()
          })
        } else {
          console.log('验证失败');
          this.$u.toast('请检查是否有未填项目')
        }
      });
    },
    handleReject(){
      let userInfo = this.$storage.get(this.$rp.common.USER).user.personName
      let toDay = util.dateUtils.dateFormat('yyyy-MM-dd',new Date())
      this.$modal.showLoading("加载中...");
      let params = {
        gfaCode:this.step,
        nextPersons:"",
        stepCode:"01",
        approvePersonUuid:this.$storage.get(this.$rp.common.USER).user.personUuid,
        // approveInfo:userInfo,
        approveDate:toDay,
      }
      if(this.step == '02'){
        // params.approveInfo = this.formData.condition02 ? this.formData.condition02 : "不同意"
        params.approveInfo = ''
      }else if(this.step == '03'){
        params.approveInfo = this.formData.condition03 ? this.formData.condition03 : "不同意"
      }
      approval.abnormaOverrule(this.formData,params).then(res => {
        this.$u.toast(res.msg)
        this.$modal.hideLoading();
        this.backPrev();
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.u-form {
  // padding-left: 1%;
  // padding-right: 1%;
}
.card-head {
  color: #191f25;
  font-weight: bold;
  font-size: 28rpx;
}
  .footer{
    position: fixed;
    bottom: 5px;
    left: 0;
    width: 100%;
    z-index: 10;
  }
.popup-header-item{
    background-color: rgb(41, 121, 255);
    width: 100%;
    height: 88rpx;
    font-size: 32rpx;
    font-weight: bold;
    color: white;
    line-height: 88rpx;
    text-align: center;
  }
  .header-btn{
    float: right;
    margin-right: 5%;
    font-size: 28rpx;
  }
</style>